﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;

namespace MxWeiXinPF.Web.shopmgr.coupon
{
    public partial class coupon_edit : Web.UI.ManagePage
    {
        private string action = MXEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = MXRequest.GetQueryString("action");

            this.id = MXRequest.GetQueryInt("id");


            if (!string.IsNullOrEmpty(_action) && _action == MXEnums.ActionEnum.Edit.ToString())
            {
                this.action = MXEnums.ActionEnum.Edit.ToString();//修改类型
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back", "Error");
                    return;
                }
                if (!new BLL.wx_shop_coupon().Exists(this.id))
                {
                    JscriptMsg("优惠券不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                //  ChkAdminLevel("channel_" + this.channel_name + "_category", MXEnums.ActionEnum.View.ToString()); //检查权限
                TreeBind(); //绑定类别
                // this.btnSubmit.Visible = ChkAdminLevelEx("category_list", MXEnums.ActionEnum.Edit.ToString());
                if (action == MXEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
                else
                {
                }
            }
        }

        #region 绑定类别=================================
        private void TreeBind()
        {

        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.wx_shop_coupon bll = new BLL.wx_shop_coupon();
            Model.wx_shop_coupon model = bll.GetModel(_id);

            txtTitle.Text = model.couponname;
            tx_money.Text = model.money.ToString();
            txtValidity.Text = model.timeend.ToString("yyyy-MM-dd HH:mm:ss");

            if (model.status == 0)
            {
                this.rdbIsActive.SelectedIndex = 1;
            }
            else
            {
                this.rdbIsActive.SelectedIndex = 0;
            }
            txtimgCouponCanUseIco.Text = model.thumb;
            txtCouponExpireIndexICO.Text = model.expiredthumb;
            txtImgCouponUsedICO.Text = model.usedthumb;
            if (model.thumb != null && model.thumb.Trim() != "")
            {
                imgCouponCanUseUrl.ImageUrl = model.thumb;
                imgCouponCanUseIco.ImageUrl = model.thumb;
            }
            if (model.expiredthumb != null && model.expiredthumb.Trim() != "")
            {
                imgCouponExpireIndexUrl.ImageUrl = model.expiredthumb;
                imgCouponExpireIco.ImageUrl = model.expiredthumb;
            }
            if (model.usedthumb != null && model.usedthumb.Trim() != "")
            {
                imgCouponUsedUrl.ImageUrl = model.usedthumb;
                imgCouponUsedIco.ImageUrl = model.usedthumb;
            }

            txtContent.Text = model.description;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            try
            {
                Model.wx_shop_coupon model = new Model.wx_shop_coupon();
                BLL.wx_shop_coupon bll = new BLL.wx_shop_coupon();
                model.couponname = txtTitle.Text.Trim();
                model.description = txtContent.Text.Trim();
                model.money = Convert.ToDecimal(tx_money.Text.Trim().ToString());
                model.timeend = Convert.ToDateTime(txtValidity.Text.Trim());
                model.status = Convert.ToInt32(rdbIsActive.SelectedValue);
                model.thumb = txtImgCouponCanUseUrl.Text.Trim();
                model.expiredthumb = txtCouponExpireIndexUrl.Text.Trim();
                model.usedthumb = txtCouponUsedImgUrl.Text.Trim();
                model.coupontype = 0;
                model.shopid = Common.MyCommFun.GetMainShopID();
                model.enough = 1;
                model.createtime = DateTime.Now;
                model.categoryid = 0;
                model.timestart = DateTime.Now;
                model.numperuser = 1;
                model.totalnum = 0;
                if (bll.Add(model) > 0)
                {
                    AddAdminLog(MXEnums.ActionEnum.Add.ToString(), "添加优惠券:" + model.couponname); //记录日志
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            try
            {
                BLL.wx_shop_coupon bll = new BLL.wx_shop_coupon();
                Model.wx_shop_coupon model = bll.GetModel(_id);
                model.couponname = txtTitle.Text.Trim();
                model.description = txtContent.Text.Trim();
                model.money = Convert.ToDecimal(tx_money.Text.Trim().ToString());
                model.timeend = Convert.ToDateTime(txtValidity.Text.Trim());
                model.status = Convert.ToInt32(rdbIsActive.SelectedValue);
                model.thumb =  Request.Form["txtimgCouponCanUseIco"].Trim();
                model.expiredthumb =  Request.Form["txtCouponExpireIndexICO"].Trim();
                model.usedthumb =  Request.Form["txtImgCouponUsedICO"].Trim();
                model.coupontype = 0;
                model.shopid = Common.MyCommFun.GetMainShopID();
                model.enough = 1;
                model.createtime = DateTime.Now;
                model.categoryid = 0;
                model.timestart = DateTime.Now;
                model.numperuser = 1;
                model.totalnum = 0;
                if (bll.Update(model))
                {
                    AddAdminLog(MXEnums.ActionEnum.Edit.ToString(), "修改优惠券:" + model.couponname); //记录日志
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
        #endregion

        //保存类别
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == MXEnums.ActionEnum.Edit.ToString()) //修改
            {
                // ChkAdminLevel("channel_" + this.channel_name + "_category", MXEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {

                    JscriptMsg("保存过程中发生错误！", "", "Error");
                    return;
                }
                JscriptMsg("修改修改前成功！", "coupon_list.aspx", "Success");
            }
            else //添加
            {
                // ChkAdminLevel("channel_" + this.channel_name + "_category", MXEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "", "Error");
                    return;
                }
                JscriptMsg("添加优惠券成功！", "coupon_list.aspx", "Success");
            }
        }
    }
}